package com.kbckbc.emailmemo;

import java.util.ArrayList;

import android.content.Context;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class MemoListAdapter extends BaseAdapter {

	final static int MEMO_SUBJECT_LEN = 15;

	final static String MEMO_SORT_ORDER_KEY = "sortorder";
	final static String MEMO_SORT_ORDER_BY_CREATE = "1";
	final static String MEMO_SORT_ORDER_BY_MODIFY = "2";

	Context maincon;
	LayoutInflater inflater;
	ArrayList<Memo> memoArray;
	int layout;

	public MemoListAdapter(Context _context, int _layout, ArrayList<Memo> _src) {
		maincon = _context;
		layout = _layout;
		memoArray = _src;
		inflater = (LayoutInflater) _context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return memoArray.size();
	}

	public String getItem(int position) {
		return memoArray.get(position).content;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(layout, parent, false);
		}

		CheckBox cbCheck = (CheckBox) convertView.findViewById(R.id.check);
		TextView txtContent = (TextView) convertView.findViewById(R.id.content);
		TextView txtId = (TextView) convertView.findViewById(R.id._id);
		TextView txtTag = (TextView) convertView.findViewById(R.id.tag);
		TextView txtDttm = (TextView) convertView.findViewById(R.id.dttm);
		ImageView imgSync = (ImageView) convertView.findViewById(R.id.sync);
		
		// fill the controls with contents.
		Memo currMemo = memoArray.get(position);

		cbCheck.setChecked(currMemo.checked);

		String content = currMemo.content.replace("\n", " ");
		content = (content.length() >= MEMO_SUBJECT_LEN ? content.substring(0,
				MEMO_SUBJECT_LEN) + "..." : content);
		txtContent.setText(content);

		txtId.setText(Integer.toString(currMemo._id) + ",");

		txtTag.setText("tag - " + currMemo.tag);

		String sortOrder = PreferenceManager.getDefaultSharedPreferences(
				maincon).getString(MEMO_SORT_ORDER_KEY,
				MEMO_SORT_ORDER_BY_CREATE);
		if (sortOrder.equals(MEMO_SORT_ORDER_BY_CREATE)) {
			txtDttm.setText("c - " + currMemo.getCreateDttm());
		} else {
			txtDttm.setText("m - " + currMemo.getModifyDttm());
		}

		cbCheck.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				setCheckOne(position);
			}
		});

		L.v("getView called - " + currMemo.getSubject() + ", sync - " + currMemo.getSync());
		if("".equals(currMemo.getSync())) {
			imgSync.setImageResource(R.drawable.unsync48);
		}
		else {
			imgSync.setImageResource(R.drawable.sync48);
		}
		
		return convertView;
	}

	public void setCheckOne(int position) {
		memoArray.get(position).checked = !memoArray.get(position).checked;
	}

	public void setCheckAll() {
		if (memoArray.size() != 0) {
			boolean firstCheck = memoArray.get(0).checked;
			for (int i = 0; i < memoArray.size(); i++) {
				memoArray.get(i).checked = !firstCheck;
			}

			notifyDataSetChanged();
		}
	}

	public ArrayList<Integer> getSelectedItemIds() {
		ArrayList<Integer> ids = new ArrayList<Integer>();

		for (int i = 0; i < memoArray.size(); i++) {
			if (memoArray.get(i).checked == true) {
				ids.add(i);
			}
		}

		return ids;
	}
}
